﻿using System;
using System.Data.SqlClient;
using System.Web.Mvc;
using Microsoft.Web.Mvc;
using SapaFinance.DTO;
using SapaFinance.Services;
using Telerik.Web.Mvc;

namespace SapaFinance.UI.Controllers
{
       
    public class BankController : Controller
    {
        private readonly IBankService _bankService;

        public BankController(IBankService bankService)
        {
            _bankService = bankService;
        }


        public virtual ActionResult RenderBank(Guid id)
        {
            return View("EditorTemplates/BankList", _bankService.GetSelectedListById(id));
        }
        public virtual ActionResult Index()
        {
            return View("Index", _bankService.GetListDTO());
        }
        [HttpPost]
        [GridAction]
        public virtual ActionResult Insert(BankDTO dto)
        {
            dto.CurrentUser = User.Identity.Name;
            if (ModelState.IsValid){
                _bankService.Insert(dto);
                return this.RedirectToAction(x => x.Index());
            }
            return Index();
        }

        [HttpPost]
        [GridAction]
     
        public virtual ActionResult Delete(Guid id)
        {
                _bankService.Delete(id);
            return this.RedirectToAction(x => x.Index());
        }

        [HttpPost]
        [GridAction]
        public virtual ActionResult Update(BankDTO dto)
        {
            dto.CurrentUser = User.Identity.Name;
            if (ModelState.IsValid){
                _bankService.Update(dto);
                return this.RedirectToAction(x => x.Index());
            }
            return Index();
        }
    }
}